package com.shanzmoo.core.domain;

import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.core.util.BCryptUtils;
import com.shanzmoo.db.sys.entity.UserEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统用户保存对象
 * @author: Gzhao 2020/11/12
 */
@Data
@ApiModel(value = "用户保存对象")
public class UserSaveDto {

    @NotEmpty(message = "用户名称不允许为空")
    @ApiModelProperty(name = "username", value = "用户名称" )
    private String username;

    @ApiModelProperty(name = "outletId", value = "门店id" )
    private Integer outletId;

    @NotEmpty(message = "角色id列表不允许为空")
    @ApiModelProperty("角色id列表" )
    private List<Integer> roleIds;

    public static UserEntity converted(UserSaveDto saveDto) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(saveDto.getUsername().trim());
        userEntity.setOutletId(saveDto.getOutletId());
        userEntity.setStatus(true);
        userEntity.setPassword(BCryptUtils.createInitPassword());
        userEntity.setCreateUser(CurrentUserCache.getUserId());
        return userEntity;
    }
}
